from DrissionPage import Chromium
from DrissionPage.common import Settings
import time

Settings.set_language('zh_cn')  # 设置为中文
file_path = "D:\SpiderData\OpenHarmony开发者论坛.txt"

# 启动或接管浏览器，并创建标签页对象
tab = Chromium().latest_tab
# 这之后出现的弹窗都会自动确认
tab.browser.set.auto_handle_alert()
# 跳转到openharmony开源社区
tab.get('https://forums.openharmony.cn/')

search_keys = ["动画", "动效", "animation", "animateTo"]

for search_key in search_keys:
    # 等待搜索输入框和搜索按钮渲染完成
    tab.wait.ele_displayed('@name=srchtxt')
    tab.wait.ele_displayed('@name=searchsubmit')
    # 定位到搜索框，获取文本框元素：通过标签类型和name属性定位
    ele = tab.ele('@@tag()=input@@name=srchtxt')
    # 输入检索关键词
    ele.input(search_key, clear = True)
    # 点击搜索按钮
    tab.ele('@@tag()=button@@name=searchsubmit').click()

    while True:
        # 进入搜索结果页，并等待页面渲染完成
        tab.wait.ele_displayed('@id=threadlist')
        all_links = []

        for parent in tab.eles('@tag()=h5'):
            link = parent.ele('@tag()=a')
            if link:
                all_links.append(link)

        for link in all_links:
            link_tab = link.click.for_new_tab()
            link_tab.wait.ele_displayed('@tag()=html')
            if not link_tab.ele('@tag()=html'):
                continue
            time.sleep(3)
            link_tab.wait.ele_displayed('@id=thread_subject')
            link_tab.ele('@tag()=html').scroll.to_bottom()

            print(f"""
            标题：{link.text}
            链接：{link.link}
            """)

            time.sleep(3)
            comments = link_tab.eles('@@class=ssfv@!key=0', timeout=2)
            if comments:
                with open(file_path, 'a', encoding='utf-8') as f:
                    f.write("标题：" + link.text + "\n")
                    f.write("链接：" + link.link + "\n")
                    f.write("评论：\n")
                for comment in comments:
                    with open(file_path, 'a', encoding='utf-8') as f:
                        f.writelines(comment.texts())
                        f.write("\n")
                with open(file_path, 'a', encoding='utf-8') as f:
                    f.write("\r\n\r\n")

            link_tab.close()
            time.sleep(2)

        next_page_ele = tab.ele('@@class=nxt@@text()=下一页')
        if next_page_ele:
            next_page_ele.click()
        else:
            break
